//
//  User.swift
//  物料商行
//
//  Created by HaoDong Hong on 2017/8/13.
//  Copyright © 2017年 HaoDong Hong. All rights reserved.
//

import Foundation
import UIKit

public struct User {
    static var sessionID: String? {
        get {
            return UserDefaults.standard.string(forKey: "sessionID")
        }
        set {
            UserDefaults.standard.set(newValue, forKey: "sessionID")
        }
    }
    static var phoneNumber: String? {
        get {
            return UserDefaults.standard.string(forKey: "phoneNumber")!
        }
        set {
            UserDefaults.standard.set(newValue, forKey: "phoneNumber")
        }
    }
    static var isLogin: Bool {
        get {
            return UserDefaults.standard.bool(forKey: "isLogin")
        }
        set {
            UserDefaults.standard.set(newValue, forKey: "isLogin")
        }
    }
    static var userName: String? {
        get {
            if let name = UserDefaults.standard.string(forKey: "userName") {
                return name
            } else {
                return "yourName"
            }
        }
        set {
            UserDefaults.standard.set(newValue, forKey: "userName")
        }
    }
    static var email: String? {
        get {
            if let email = UserDefaults.standard.string(forKey: "email") {
                return email
            } else {
                return "yourEmail@xxx.com"
            }
        }
        set {
            UserDefaults.standard.set(newValue, forKey: "email")
        }
    }
    
    
    static func clearUserData() {
        let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
        let imagePath = documentsURL.appendingPathComponent("icon").path
        if FileManager.default.fileExists(atPath: imagePath) {
            do {
                try FileManager.default.removeItem(atPath: imagePath)
            }
            catch let error {
                print(error)
            }
        } else {
            print("file does not exist")
        }
        sessionID = nil
        phoneNumber = nil
        isLogin = false
        userName = nil
        email = nil
    }
}

